#include "AlgorithmIce.h"

#include <Ice/Ice.h>
#include <IceUtil/IceUtil.h>

#include "json.hpp"
#include <memory>
#include <iostream>
#include <thread>
#include <chrono>
#include <sstream>
#include <string>

using namespace nlohmann;
class AlgorithmIceServer:public AlgorithmIce::Server
{
public:
    AlgorithmIceServer(){}
    void addClient(::std::shared_ptr<AlgorithmIce::ClientPrx> client, const ::Ice::Current& current){
        std::cout<<std::this_thread::get_id()<<std::endl;
        std::thread([=]{
            try{
                json init_data;
                init_data["EngineNum"] = "XX20-001";
                std::shared_ptr<AlgorithmIce::ClientPrx> pClient = client->ice_fixed(current.con);
                pClient->Register();
                pClient->Init(init_data.dump());
                while(true){
                    printf("-----------start\n");
                    pClient->Start("");
                    json calc_data, source_data, data;
                    data["Value"] = 9;
                    data["Name"] = u8"paramINT";
                    source_data["Data"].push_back(data);
                    data["Value"] = rand()*3.0/RAND_MAX;
                    data["Name"] = u8"T2_D";
                    source_data["Data"].push_back(data);
                    data["Value"] = rand()*2.0/RAND_MAX;
                    data["Name"] = u8"Tohz";
                    source_data["Data"].push_back(data);
                    source_data["Len"] = 3;
                    source_data["Id"] = 0;
                    source_data["SourceName"] = u8"s1";
                    calc_data["Source"].push_back(source_data);
                    for(int i=0;i<1000;i++){
                        pClient->Notice(0,calc_data.dump());
                    }
                    pClient->Stop("");
                    printf("-----------close\n");
                }
                pClient->Close("");
            }catch(std::exception& e){
                std::cout<<(e.what())<<std::endl;
            }
        }).detach();
    }
    void delClient(::std::shared_ptr<AlgorithmIce::ClientPrx> client, const ::Ice::Current& current){
        std::cout<<std::this_thread::get_id()<<"--------delclient"<<std::endl;
    }
    void Ask(int nEventId, ::std::string strData, const ::Ice::Current& current){
        //std::cout<<std::this_thread::get_id()<<"--------ask"<<std::endl;
        std::cout<<std::this_thread::get_id()<<"--------"<<strData<<std::endl;
    }
};

int main()
{
    try{
        Ice::CommunicatorPtr communicator = Ice::initialize();
        Ice::ObjectAdapterPtr pAdapter = communicator->createObjectAdapterWithEndpoints("Server.Proxy", "default -p 10091");

        std::shared_ptr<AlgorithmIceServer> server = std::make_shared<AlgorithmIceServer>();
        Ice::Identity id = Ice::stringToIdentity("server");
        pAdapter->add(server, id);

        pAdapter->activate();
        communicator->waitForShutdown();
    }
    catch (const Ice::Exception& e)
    {
        std::cerr << e.what() << std::endl;
    }
    catch (std::exception& e)
    {        
        std::cerr << e.what() << std::endl;
    }
    return 0;
}